package com.itfreer.print.base.itextpdf.textbefore;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itfreer.print.base.itextpdf.ITextBefore;

/**
 * 设置PDF密码和只读模式
 */
public class PasswordTextBefore implements ITextBefore {
	
	private String passwordString;
	
	public PasswordTextBefore(){
		
	}
	
	public PasswordTextBefore(String passwordString){
		this.passwordString = passwordString;
	}
	
	/**
	 * 文档操作
	 * @param pdfDocument
	 * @param pdfWriter
	 */
	public void doOptions(Document pdfDocument, PdfWriter pdfWriter){
		if (pdfWriter == null) {
			return;
		}
		try {
			if(passwordString!=null){
				pdfWriter.setEncryption(passwordString.getBytes(), passwordString.getBytes(), 
						PdfWriter.ALLOW_COPY | PdfWriter.ALLOW_PRINTING,PdfWriter.STANDARD_ENCRYPTION_128);
			}else{
				pdfWriter.setEncryption(null, null, 
						PdfWriter.ALLOW_COPY | PdfWriter.ALLOW_PRINTING,PdfWriter.STANDARD_ENCRYPTION_128);
			}
			
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}
}
